/**
 * Created by chesw on 2017/3/31.
 */
//页面加载完成，显示整个页面
function showHtml() {
    Vue.nextTick(function () {
        var elem = document.getElementsByTagName('body');
        elem[0].style.display = 'block';
    })
}
//解析url参数
function paramUrl(p_url) {
    if (!p_url) return;

    var tempUrl = null, param = null, paramArr = {};
    tempUrl = p_url.split('?')[1];
    if (tempUrl) {
        param = tempUrl.split('&');
        for (var i = 0; i < param.length; i++) {
            var temp = param[i].split('=');
            paramArr[temp[0]] = temp[1];
        }
    }
    return paramArr;
}
function textOverflow() {
    $('.two-overflow').xdlines({
        max: 2  //设置文本的最大行数
    });
    $('.three-overflow').xdlines({
        max: 3  //设置文本的最大行数
    });
    $('.four-overflow').xdlines({
        max: 5  //设置文本的最大行数
    });
}

/* 提示框
 * p_obj: {url: 'logo', text:'提示文字'}
 * */
function dialogTip(p_obj) {
    var _html = null;
    var _doc = document.getElementsByClassName('html-alert');
    if (_doc.length <= 0) {
        _html = document.createElement('div');
        _html.className = 'html-alert';
        _html.innerHTML = '<figure class="tip_box">' +
            '<img src="' + p_obj.url + '">' +
            '<p class="title">' + p_obj.text + '</p>' +
            '</figure>';
        document.body.appendChild(_html);
    } else {
        _html = document.createElement('figure');
        _html.className = 'tip_box';
        _html.innerHTML = '<img src="' + p_obj.url + '">' +
            '<p class="title">' + p_obj.text + '</p>';
        _doc[0].appendChild(_html);
    }

    var _newDoc = document.getElementsByClassName('html-alert');
    if (p_obj.text.length <= 20) { //提示文字长度小于等于20则1秒后消失
        setTimeout(function () { _newDoc[0].parentNode.removeChild(_newDoc[0]); }, 1000);
    }
    else if (p_obj.text.length > 20) {//提示文字长度大于20则2秒后消失
        setTimeout(function () { _newDoc[0].parentNode.removeChild(_newDoc[0]); }, 2000);
    }
}

//生成和验证验证码
var code;
function createCode() 
{
    code = "";
    var codeLength = 4; //验证码的长度
    var checkCode = document.getElementById("checkCode");
    var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
         'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //所有候选组成验证码的字符，当然也可以用中文的
    for(var i = 0; i < codeLength; i++) 
    {
        var charNum = Math.floor(Math.random() * 52);
        code += codeChars[charNum];
    }
    if(checkCode) 
    {
        checkCode.className = "code";
        checkCode.innerHTML = code;
    }
}
//处理显示注册和登录页面
function checkLoginType(param)
{
    var arr = param.substring(1).split("=");
    var loginType = arr[1];
    var loginTab = document.getElementById("loginTab");
    var registerTab = document.getElementById("registerTab");
    var login = document.getElementById("login");
    var register = document.getElementById("register");
    var tipBox = document.getElementById("tipBox");

    if(loginType == "reg")
    {
        loginTab.removeAttribute("class", "navActive");
        registerTab.setAttribute("class", "navActive");
        login.style.display = "none";
        registerSuccess.style.display = "none";
        register.style.display = "block";
        tipBox.style.display = "none";
    }
    else
    {
        loginTab.setAttribute("class", "navActive");
        registerTab.removeAttribute("class", "navActive");
        login.style.display = "block";
        register.style.display = "none";
        tipBox.style.display = "none";
    }
}

var clock = '';
var nums = 60;
var btn;
function sendCode(thisBtn) {
    btn = thisBtn;
    btn.disabled = true; //将按钮置为不可点击
    btn.innerHTML = nums + "s 后重新获取";
    clock = setInterval(doLoop, 1000); //一秒执行一次
}
function doLoop() {  
    nums--;
    if (nums > 0) {
        btn.innerHTML = nums + "s 后重新获取";
    } else {
        clearInterval(clock); //清除js定时器
        btn.disabled = false;
        btn.innerHTML = "获取验证码";
        nums = 60; //重置时间
    }
}

function checkValidateCode(_self)
{
    varRet = true;
    tipBox = document.getElementById("tipBox");
    _self.$http.post("/Login/GetCurrentValidateCode").then(function (ret) {
        if (ret.body.Code == 1) {
            currentValCode = ret.body.Msg;
            varRet = false;
        }
        else {
            tipBox.style.display = "block";
            _self.sLoginTip = ret.body.Msg;
            varRet = false;
        }
        return varRet;
    })   
}


function CheckLoginStatus(_self, loginInfor)
{
    _self.$http.post("/Home/CheckLoginStatus").then(function (res) {
        if (res.body != null) {
            loginInfor.isLogin = res.body.isLogin;
            loginInfor.name = res.body.userName;
            loginInfor.mobile = res.body.mobile;

            var reg = /^(\d{3})\d{4}(\d{4})$/;
            loginInfor.mobile = loginInfor.mobile.replace(reg, "$1****$2");

            if (loginInfor.name != null && loginInfor.name != "")
                _self.userName = loginInfor.name;
            else {
                _self.userName = loginInfor.mobile;
            }

            RegistBar = document.getElementById("RegistBar");
            LoginBar = document.getElementById("LoginBar");

            //显示指定的头菜单
            if (loginInfor.isLogin == true) {
                RegistBar.style.display = "none";
                LoginBar.style.display = "block";
            }
            else {
                RegistBar.style.display = "block";
                LoginBar.style.display = "none";

                if (window.location.href.indexOf("PersonalCenter") != -1)
                    window.location.href = "../Login/Index";
            }
        }
    });
}
//带按钮弹窗
var dialogConfirm = Vue.extend({
    props: ['param'],
    template: '<div class="dialog-confirm" v-if="param.type">' +
        '<div class="black-cloth"></div>' +
        '<div class="dialog-confirm-inner">' +
            '<div class="head">' +
                '<p class="title">温馨提示' +
                '<i class="confirm-close" @click="param.type=false"></i></p>' +
            '</div>' +
            '<p class="message">{{param.text}}</p>' +
            '<div class="btn">' +
                '<span class="btn-left common-confirm-btn" @click="param.left_func">{{param.l_val}}</span>' +
                '<span class="btn-right common-confirm-btn" @click="param.type=false">{{param.r_val}}</span>' +
            '</div>' +
        '</div>' +
    '</div>'
});
//左边导航栏，切换颜色修改
function changeColorByLNav(p_obj, p_index1, p_index2) {
    for (var i = 0 ; i < p_obj.tabList.length; i++) {
        for (var j = 0; j < p_obj.tabList[i].list.length; j++) {
            if (i == p_index1 && j == p_index2) {
                p_obj.tabList[i].list[j].cls = GlobalValue.VALIDTAB;
            } else {
                p_obj.tabList[i].list[j].cls = GlobalValue.INVALIDTAB;
            }
        }
    }
    return p_obj;
}